﻿using FieldTools.Client.Business.IBusiness;
using FieldTools.Util;
using HtmlAgilityPack;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FieldTools.Client.Business.Business
{
    public class XMLBus : IXMLBus, ITransientDependency
    {
        private readonly ILogger _logger;
        public XMLBus(ILogger<XMLBus> logger)
        {
            _logger = logger;
        }

        /// <inheritdoc/>
        public string Crawl(string sourceDoc, int pathMode, string path, int type, string attributes)
        {
            if (string.IsNullOrEmpty(sourceDoc))
            {
                return "源码为空，无法询到节点";
            }

            string ShowDoc = "";
            if (pathMode == 1)
            {
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(sourceDoc);
                var nodes = XmlHelper.GetHtmlNodes(doc, path);
                if (nodes == null)
                {
                    return "未查询到节点";
                }
                foreach (var node in nodes)
                {
                    string text = "";
                    switch (type)
                    {
                        case 1: text = node.OuterHtml; break;
                        case 2: text = node.InnerHtml; break;
                        case 3: text = node.InnerText; break;
                        default: break;
                    }
                    if (attributes != null && attributes != "")
                    {
                        ShowDoc += node.Attributes[attributes].Value + "\r\n";
                    }
                    else
                    {
                        ShowDoc += text + "\r\n";
                    }
                }
            }
            else if (pathMode == 2)
            {
                var ss = XmlHelper.GetElementsByAngleSharp(sourceDoc, path);
                if (ss == null)
                {
                    return "未查询到节点";
                }
                foreach (var e in ss)
                {
                    string text = "";
                    switch (type)
                    {
                        case 1: text = e.OuterHtml; break;
                        case 2: text = e.InnerHtml; break;
                        case 3: text = e.TextContent; break;
                        default: break;
                    }
                    if (attributes != null && attributes != "")
                    {
                        try
                        {
                            ShowDoc += e.Attributes[attributes]?.Value + "\r\n";
                        }
                        catch (Exception ex)
                        {
                            ShowDoc += ex.Message + "\r\n";
                        }
                    }
                    else
                    {
                        ShowDoc += text + "\r\n";
                    }
                }
            }
            return ShowDoc;
        }
    }
}
